// fileName      :  irequest.go
// createdAt     :  2021-10-30
// description   :

package gin

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
)

type IRequest interface {
	DefaultQueryString(string, string) string
	DefaultQueryInt(string, int) int
	DefaultQueryInt64(string, int64) int64
	DefaultQueryInt32(string, int32) int32
	DefaultQueryIntArray(string, []int) []int
	DefaultQueryMapString() map[string]string
	DefaultQueryMapInterface() map[string]interface{}
	DefaultQueryStringArray(string, []string) []string
	DefaultQueryFloat32(string, float32) float32
	DefaultQueryFloat64(string, float64) float64
	DefaultQueryArraySplitString(string, []string) []string
	QueryAll() map[string][]string

	DefaultFormString(string, string) string
	DefaultFormInt(string, int) int
	DefaultFormInt64(string, int64) int64
	DefaultFormIntArray(string, []int) []int
	DefaultFormMapString() map[string]string
	DefaultFormMapInterface() map[string]interface{}
	DefaultFormStringArray(string, []string) []string
	DefaultFormFloat32(string, float32) float32
	DefaultFormFloat64(string, float64) float64
	DefaultFormArraySplitString(string, []string) []string
	FormAll() map[string][]string
}

func (c *Context) DefaultQueryString(key string, def string) string {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			return vals[0]
		}
	}
	return def
}

func (c *Context) DefaultQueryInt(key string, def int) int {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.Atoi(vals[0])
			return valInt
		}
	}
	return def
}

func (c *Context) DefaultQueryInt64(key string, def int64) int64 {
	val := c.DefaultQueryInt(key, int(def))
	return int64(val)
}

func (c *Context) DefaultQueryInt32(key string, def int32) int32 {
	val := c.DefaultQueryInt(key, int(def))
	return int32(val)
}

func (c *Context) DefaultQueryIntArray(key string, def []int) []int {
	queryAll := c.QueryAll()
	fmt.Println(queryAll)
	if vals, ok := queryAll[key]; ok {
		var intArr []int
		if len(vals) > 0 {
			for i := 0; i < len(vals); i++ {
				json.Unmarshal([]byte(vals[i]), &intArr)
			}
		}
		return intArr
	}
	return def
}

func (c *Context) DefaultQueryMapString() map[string]string {
	queryAll := c.QueryAll()
	var mf = map[string]string{}
	for k, v := range queryAll {
		if len(v) > 0 {
			mf[k] = v[0]
		}
	}
	return mf
}

func (c *Context) DefaultQueryMapInterface() map[string]interface{} {
	queryAll := c.QueryAll()
	var mf = map[string]interface{}{}
	for k, v := range queryAll {
		if len(v) > 0 {
			mf[k] = v[0]
		}
	}
	return mf
}

func (c *Context) DefaultQueryStringArray(key string, def []string) []string {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		return vals
	}
	return def
}

func (c *Context) DefaultQueryFloat32(key string, def float32) float32 {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.ParseFloat(string(vals[0]), 0)
			return float32(valInt)
		}
	}
	return def
}

func (c *Context) DefaultQueryFloat64(key string, def float64) float64 {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.ParseFloat(string(vals[0]), 0)
			return valInt
		}
	}
	return def
}

func (c *Context) DefaultQueryArraySplitString(key string, def []string) []string {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valArr := strings.Split(vals[0], ",")
			return valArr
		}
	}
	return def
}

func (c *Context) QueryAll() map[string][]string {
	c.initQueryCache()
	return map[string][]string(c.queryCache)
}

func (c *Context) DefaultFormString(key string, def string) string {
	queryAll := c.FormAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			return vals[0]
		}
	}
	return def
}

func (c *Context) DefaultFormInt(key string, def int) int {
	queryAll := c.FormAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.Atoi(vals[0])
			return valInt
		}
	}
	return def
}

func (c *Context) DefaultFormInt64(key string, def int64) int64 {
	queryAll := c.FormAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.Atoi(vals[0])
			return int64(valInt)
		}
	}
	return def
}

func (c *Context) DefaultFormIntArray(key string, def []int) []int {
	queryAll := c.FormAll()
	fmt.Println(queryAll)
	if vals, ok := queryAll[key]; ok {
		var intArr []int
		if len(vals) > 0 {
			for i := 0; i < len(vals); i++ {
				json.Unmarshal([]byte(vals[i]), &intArr)
			}
		}
		return intArr
	}
	return def
}

func (c *Context) DefaultFormMapString() map[string]string {
	queryAll := c.FormAll()
	var mf = map[string]string{}
	for k, v := range queryAll {
		if len(v) > 0 {
			mf[k] = v[0]
		}
	}
	return mf
}

func (c *Context) DefaultFormMapInterface() map[string]interface{} {
	queryAll := c.FormAll()
	var mf = map[string]interface{}{}
	for k, v := range queryAll {
		if len(v) > 0 {
			mf[k] = v[0]
		}
	}
	return mf
}

func (c *Context) DefaultFormStringArray(key string, def []string) []string {
	queryAll := c.QueryAll()
	if vals, ok := queryAll[key]; ok {
		return vals
	}
	return def
}

func (c *Context) DefaultFormFloat32(key string, def float32) float32 {
	queryAll := c.FormAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.ParseFloat(string(vals[0]), 0)
			return float32(valInt)
		}
	}
	return def
}

func (c *Context) DefaultFormFloat64(key string, def float64) float64 {
	queryAll := c.FormAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valInt, _ := strconv.ParseFloat(string(vals[0]), 0)
			return valInt
		}
	}
	return def
}

func (c *Context) DefaultFormArraySplitString(key string, def []string) []string {
	queryAll := c.FormAll()
	if vals, ok := queryAll[key]; ok {
		if len(vals) > 0 {
			valArr := strings.Split(vals[0], ",")
			return valArr
		}
	}
	return def
}

func (c *Context) FormAll() map[string][]string {
	c.initFormCache()
	return map[string][]string(c.formCache)
}
